import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

// 设置静态路由
const constRouters = [
  // 重定向/
  {
    path: '/',
    redirect: '/login'
  },
  // 登录
  {
    path: '/login',
    component: () => import('../views/login/Login.vue')
  },
  // 首页
  {
    path: '/home',
    meta: { title: '首页', menu: true, icon: 'icon-shouyefill' },
    component: () => import('../layout/Layout.vue'),
    children: [
      {
        path: '',
        meta: { title: '首页', menu: true },
        component: () => import('../views/home/Home.vue')
      }
    ]
  },
]

// 设置meta版本动态路由
const mateRouters = [

  // 账号
  {
    path: '/account',
    meta: { title: '账号管理', roles: ['super', 'normal'], icon: 'icon-zhanghao' },
    component: () => import('../layout/Layout.vue'),
    // redirect: '/account/list',
    children: [
      {
        path: '/account/list',
        meta: { title: '账号列表', roles: ['super', 'normal'] },
        component: () => import('../views/account/AccountList.vue')
      },
      {
        path: '/account/center',
        meta: { title: '个人中心', roles: ['super', 'normal'] },
        component: () => import('../views/account/AccountCenter.vue')
      },
      {
        path: '/account/revise',
        meta: { title: '修改密码', roles: ['super', 'normal'] },
        component: () => import('../views/account/AccountRevise.vue')
      },
      {
        path: '/account/add',
        meta: { title: '添加账号', roles: ['super'] },
        component: () => import('../views/account/AccountAdd.vue')
      }
    ]
  },
  // 商品管理
  {
    path: '/goods',
    meta: { title: '商品管理', roles: ['super', 'normal'], icon: 'icon-shangpin' },
    component: () => import('../layout/Layout.vue'),
    // redirect: '/goods/list',
    children: [
      {
        path: '/goods/list',
        meta: { title: '商品列表', roles: ['super', 'normal'] },
        component: () => import('../views/goods/GoodsList.vue')
      },
      {
        path: '/goods/add',
        meta: { title: '添加商品', roles: ['super'] },
        component: () => import('../views/goods/GoodsAdd.vue')
      },
      {
        path: '/goods/type',
        meta: { title: '商品分类', roles: ['super', 'normal'] },
        component: () => import('../views/goods/GoodsType.vue')
      }
    ]
  },
  // 订单
  {
    path: '/order',
    meta: { title: '订单管理', roles: ['super', 'normal'], icon: 'icon-dingdan-quanbudingdan' },
    component: () => import('../layout/Layout.vue'),
    children: [
      {
        path: '',
        meta: { title: '订单管理', roles: ['super', 'normal'] },
        component: () => import('../views/order/Order.vue')
      }
    ]

  },
  // 店铺
  {
    path: '/shop',
    meta: { title: '店铺管理', roles: ['super'], icon: 'icon-dianpufill' },
    component: () => import('../layout/Layout.vue'),
    children: [
      {
        path: '',
        meta: { title: '店铺管理', roles: ['super', 'normal'] },
        component: () => import('../views/shop/Shop.vue')
      }
    ]
  },
  // 销售统计
  {
    path: '/sale',
    meta: { title: '销售统计', roles: ['super', 'normal'], icon: 'icon-xiaoshoutongji' },
    component: () => import('../layout/Layout.vue'),
    // redirect: '/sale/goodsStatistics',
    children: [
      {
        path: '/sale/goodsStatistics',
        meta: { title: '商品统计', roles: ['super', 'normal'] },
        component: () => import('../views/sale/GoodsStatistics.vue')
      },
      {
        path: '/sale/orderStatistics',
        meta: { title: '订单统计', roles: ['super', 'normal'] },
        component: () => import('../views/sale/OrderStatistics.vue')
      },
      {
        path: '/sale/charts',
        meta: { title: '单页面多图表', roles: ['super', 'normal'] },
        component: () => import('../views/sale/Charts.vue')
      },
    ]
  },
]

// 设置超级管理员的动态路由
const superRouters = [
  // 商品管理
  {
    path: '/goods',
    meta: { title: '商品管理' },
    component: () => import('../layout/Layout.vue'),
    redirect: '/goods/list',
    children: [
      {
        path: '/goods/list',
        meta: { title: '商品列表' },
        component: () => import('../views/goods/GoodsList.vue')
      },
      {
        path: '/goods/add',
        meta: { title: '添加商品' },
        component: () => import('../views/goods/GoodsAdd.vue')
      },
      {
        path: '/goods/type',
        meta: { title: '商品分类' },
        component: () => import('../views/goods/GoodsType.vue')
      }
    ]
  },
  // 账号
  {
    path: '/account',
    meta: { title: '账号管理' },
    component: () => import('../layout/Layout.vue'),
    redirect: '/account/list',
    children: [
      {
        path: '/account/list',
        meta: { title: '账号列表' },
        component: () => import('../views/account/AccountList.vue')
      },
      {
        path: '/account/center',
        meta: { title: '个人中心' },
        component: () => import('../views/account/AccountCenter.vue')
      },
      {
        path: '/account/revise',
        meta: { title: '修改密码' },
        component: () => import('../views/account/AccountRevise.vue')
      },
      {
        path: '/account/add',
        meta: { title: '添加账号' },
        component: () => import('../views/account/AccountAdd.vue')
      }
    ]
  },
  // 订单
  {
    path: '/order',
    meta: { title: '订单管理' },
    component: () => import('../layout/Layout.vue'),
    children: [
      {
        path: '',
        // meta: { title: '订单管理' },
        component: () => import('../views/order/Order.vue')
      }
    ]

  },
  // 店铺
  {
    path: '/shop',
    meta: { title: '店铺管理' },
    component: () => import('../layout/Layout.vue'),
    children: [
      {
        path: '',
        // meta: { title: '店铺管理' },
        component: () => import('../views/shop/Shop.vue')
      }
    ]
  },
  // 销售统计
  {
    path: '/sale',
    meta: { title: '销售统计' },
    component: () => import('../layout/Layout.vue'),
    redirect: '/sale/goodsStatistics',
    children: [
      {
        path: '/sale/goodsStatistics',
        meta: { title: '商品统计' },
        component: () => import('../views/sale/GoodsStatistics.vue')
      },
      {
        path: '/sale/orderStatistics',
        meta: { title: '订单统计' },
        component: () => import('../views/sale/OrderStatistics.vue')
      },
      {
        path: '/sale/charts',
        meta: { title: '单页面多图表' },
        component: () => import('../views/sale/Charts.vue')
      },
    ]
  },
]

// 普通管理员的动态路由
const normalRouters = [
  // 商品管理
  {
    path: '/goods',
    meta: { title: '商品管理' },
    component: () => import('../layout/Layout.vue'),
    redirect: '/goods/list',
    children: [
      {
        path: '/goods/list',
        meta: { title: '商品列表' },
        component: () => import('../views/goods/GoodsList.vue')
      },
      {
        path: '/goods/add',
        meta: { title: '添加商品' },
        component: () => import('../views/goods/GoodsAdd.vue')
      },
      {
        path: '/goods/type',
        meta: { title: '商品分类' },
        component: () => import('../views/goods/GoodsType.vue')
      }
    ]
  },
  // 账号
  {
    path: '/account',
    meta: { title: '账号管理' },
    component: () => import('../layout/Layout.vue'),
    redirect: '/account/list',
    children: [
      {
        path: '/account/list',
        meta: { title: '账号列表' },
        component: () => import('../views/account/AccountList.vue')
      },
      {
        path: '/account/center',
        meta: { title: '个人中心' },
        component: () => import('../views/account/AccountCenter.vue')
      },
      {
        path: '/account/revise',
        meta: { title: '修改密码' },
        component: () => import('../views/account/AccountRevise.vue')
      },
    ]
  },
  // 订单
  {
    path: '/order',
    meta: { title: '订单管理' },
    component: () => import('../layout/Layout.vue'),
    children: [
      {
        path: '',
        // meta: { title: '订单管理' },
        component: () => import('../views/order/Order.vue')
      }
    ]

  },
  // 404
  {
    path: '/*',
    component: () => import('../views/error/Error404.vue')
  }
]

const routes = [
  // 重定向/
  {
    path: '/',
    redirect: '/login'
  },
  // 登录
  {
    path: '/login',
    component: () => import('../views/login/Login.vue')
  },
  // 首页
  {
    path: '/home',
    // meta: { title: '首页' },
    component: () => import('../layout/Layout.vue'),
    children: [
      {
        path: '',
        // meta: { title: '首页' },
        component: () => import('../views/home/Home.vue')
      }
    ]
  },
  // 商品管理
  {
    path: '/goods',
    meta: { title: '商品管理' },
    component: () => import('../layout/Layout.vue'),
    redirect: '/goods/list',
    children: [
      {
        path: '/goods/list',
        meta: { title: '商品列表' },
        component: () => import('../views/goods/GoodsList.vue')
      },
      {
        path: '/goods/add',
        meta: { title: '添加商品' },
        component: () => import('../views/goods/GoodsAdd.vue')
      },
      {
        path: '/goods/type',
        meta: { title: '商品分类' },
        component: () => import('../views/goods/GoodsType.vue')
      }
    ]
  },
  // 账号
  {
    path: '/account',
    meta: { title: '账号管理' },
    component: () => import('../layout/Layout.vue'),
    redirect: '/account/list',
    children: [
      {
        path: '/account/list',
        meta: { title: '账号列表' },
        component: () => import('../views/account/AccountList.vue')
      },
      {
        path: '/account/center',
        meta: { title: '个人中心' },
        component: () => import('../views/account/AccountCenter.vue')
      },
      {
        path: '/account/revise',
        meta: { title: '修改密码' },
        component: () => import('../views/account/AccountRevise.vue')
      },
      {
        path: '/account/add',
        meta: { title: '添加账号' },
        component: () => import('../views/account/AccountAdd.vue')
      }
    ]
  },
  // 订单
  {
    path: '/order',
    meta: { title: '订单管理' },
    component: () => import('../layout/Layout.vue'),
    children: [
      {
        path: '',
        // meta: { title: '订单管理' },
        component: () => import('../views/order/Order.vue')
      }
    ]

  },
  // 店铺
  {
    path: '/shop',
    meta: { title: '店铺管理' },
    component: () => import('../layout/Layout.vue'),
    children: [
      {
        path: '',
        // meta: { title: '店铺管理' },
        component: () => import('../views/shop/Shop.vue')
      }
    ]
  },
  // 销售统计
  {
    path: '/sale',
    meta: { title: '销售统计' },
    component: () => import('../layout/Layout.vue'),
    redirect: '/sale/goodsStatistics',
    children: [
      {
        path: '/sale/goodsStatistics',
        meta: { title: '商品统计' },
        component: () => import('../views/sale/GoodsStatistics.vue')
      },
      {
        path: '/sale/orderStatistics',
        meta: { title: '订单统计' },
        component: () => import('../views/sale/OrderStatistics.vue')
      },
      {
        path: '/sale/charts',
        meta: { title: '单页面多图表' },
        component: () => import('../views/sale/Charts.vue')
      },
    ]
  },
  // 404
  {
    path: '/*',
    component: () => import('../views/error/Error404.vue')
  }
]

const router = new VueRouter({
  routes: constRouters
})

// 过滤路由列表
function filterRouter(routers, role) {
  return routers.filter(route => {
    if (route.children && route.children.length > 0) {
      route.children = filterRouter(route.children, role)
    }
    return route.meta.roles.includes(role)
    // console.log(route.meta);
  })
}

export function addRouteByRole() {
  let role = sessionStorage.getItem('role')
  if (!role) return []
  let roleArr = filterRouter(mateRouters, role)
  roleArr.forEach(route => {
    router.addRoute(route)
  })
  // 404
  router.addRoute({
    path: '/*',
    component: () => import('../views/error/Error404.vue')
  })
}

export function getMenulistByRoute() {
  const role = sessionStorage.getItem('role')
  if (!role) return []
  let arr = constRouters.filter(route => route.meta && route.meta.menu)
  return [...arr, ...filterRouter(mateRouters, role)]
  // let newArr = [...arr, ...filterRouter(mateRouters, role)]
  // console.log(newArr);
}


addRouteByRole()
// export function roleRouter() {
//   let role = sessionStorage.getItem('role')
//   if (!role) return
//   if (role == 'super') {
//     superRouters.forEach(route => {
//       router.addRoute(route)
//     })
//   } else {
//     normalRouters.forEach(route => {
//       router.addRoute(route)
//     })
//   }
// }

// 刷新执行一次
// roleRouter()

// 路由守卫
// 设置白名单
let whiteList = ['/login', '/404', 'register']
router.beforeEach((to, from, next) => {
  // debugger
  if (whiteList.includes(to.fullPath)) return next()

  let token = sessionStorage.getItem('token')
  if (token) {
    next()
  } else {
    next('/login')
  }
})

export default router
